//----------------------------------------------------------------------------------------------------
//
//	KeyDownBrowserInspector
//
//	Inherits From:		IBInspector
//
//	Declared In:		KeyDownBrowserInspector.h
//
//	Disclaimer
//
//		You may freely copy, distribute and reuse this software and its
//		associated documentation. I disclaim any warranty of any kind, 
//		expressed or implied, as to its fitness for any particular use.
//
//----------------------------------------------------------------------------------------------------
#import "KeyDownBrowserInspector.h"
#import "KeyDownBrowser.h"
#import <appkit/appkit.h>


@implementation KeyDownBrowserInspector

//----------------------------------------------------------------------------------------------------
//	Initialization and Freeing
//----------------------------------------------------------------------------------------------------
- init
{
    	char 	path[MAXPATHLEN+1];
    	id 		bundle;
		
    	[super init];
    	if (! (bundle = [NXBundle bundleForClass:[KeyDownBrowserInspector class]])) return nil;
    	if (! ([bundle getPath: path forResource:"KeyDownBrowserInspector" ofType:"nib"])) return nil;
       	[NXApp loadNibFile:path owner:self withNames:NO ]; 
    	return self;
}


//----------------------------------------------------------------------------------------------------
//	IB Inspector Methods
//----------------------------------------------------------------------------------------------------
- ok:sender
{
	if (sender == horizontalScrolling)
		[self setHorizontalScrolling];
	else if (sender == verticalScrolling)
		[self setVerticalScrolling];
	else if (sender == options)
		[self setOptions];
	else if (sender == caseSensitiveSearch)
		[self setCaseSensitiveSearch];
	else if (sender == inputTimeOut)
		[self setInputTimeOut];

	[[NXApp activeDocument] touch];
    	return [super ok:sender];
}


- revert:sender
{
	[self revertHorizontalScrolling];
	[self revertVerticalScrolling];
	[self revertOptions];
	[self revertCaseSensitiveSearch];
	[self revertInputTimeOut];
	return [super revert: sender];
}


- (BOOL)wantsButtons
{
    	return NO;
}


//----------------------------------------------------------------------------------------------------
//	Reverting Attributes
//----------------------------------------------------------------------------------------------------
- revertHorizontalScrolling
{
	[horizontalScrolling clearSelectedCell];
	if ([object isHorizontalScrollerEnabled])
		{
		[horizontalScrolling setState: 0  at: 0 :0];
		[horizontalScrolling setState: 1  at: 1 :0];
		[horizontalScrolling setState: 0  at: 2 :0];
		[[verticalScrolling findCellWithTag: 1] setEnabled: NO];
		}
	else if ([object areHorizontalScrollButtonsEnabled])
		{
		[horizontalScrolling setState: 0  at: 0 :0];
		[horizontalScrolling setState: 0  at: 1 :0];
		[horizontalScrolling setState: 1  at: 2 :0];
		[[verticalScrolling findCellWithTag: 1] setEnabled: YES];
		}
	else
		{
		[horizontalScrolling setState: 1  at: 0 :0];
		[horizontalScrolling setState: 0  at: 1 :0];
		[horizontalScrolling setState: 0  at: 2 :0];
		[[verticalScrolling findCellWithTag: 1] setEnabled: YES];
		}

	return self;
}


- revertVerticalScrolling
{
	NXRect	browserFrame;
	NXRect	columnFrame;
	NXRect	titleFrame;
	float		titleFudgeFactor = 0.0;

	[verticalScrolling clearSelectedCell];

	[object getFrame: &browserFrame];
	[object getFrame: &columnFrame  ofColumn: 0];
	[object getTitleFrame: &titleFrame  ofColumn: 0];
	if ( NX_HEIGHT(&titleFrame) > 1.0 )
		titleFudgeFactor = 2.0;

	if ([object isHorizontalScrollerEnabled])
		{
		[verticalScrolling setState: 1  at: 0 :0];
		[verticalScrolling setState: 0  at: 1 :0];
		[[horizontalScrolling findCellWithTag: 1] setEnabled: YES];
		}
	else if ( (NX_HEIGHT(&columnFrame) + NX_HEIGHT(&titleFrame) + titleFudgeFactor) == 			NX_HEIGHT(&browserFrame) )
		{
		[verticalScrolling setState: 1  at: 0 :0];
		[verticalScrolling setState: 0  at: 1 :0];
		[[horizontalScrolling findCellWithTag: 1] setEnabled: YES];
		}
	else
		{
		[verticalScrolling setState: 0  at: 0 :0];
		[verticalScrolling setState: 1  at: 1 :0];
		[[horizontalScrolling findCellWithTag: 1] setEnabled: NO];
		}

	return self;
}


- revertOptions
{
	[options setState: [object isMultipleSelectionEnabled] at: 0 :0];
	[options setState: [object isEmptySelectionEnabled] at: 1 :0];
	[options setState: [object isBranchSelectionEnabled] at: 2 :0];
	[options setState: [object columnsAreSeparated] at: 3 :0];
	[options setState: [object isTitled] at: 4 :0];
	[[options findCellWithTag: 3] setEnabled: NO];
	return self;
}


- revertCaseSensitiveSearch
{
	[caseSensitiveSearch setState: [object wantsCaseSensitiveSearch]];
	return self;
}


- revertInputTimeOut
{
	[inputTimeOut setFloatValue: [object inputTimeOut]];
	return self;
}


//----------------------------------------------------------------------------------------------------
//	Setting Attributes
//----------------------------------------------------------------------------------------------------
- setHorizontalScrolling
{
	switch ([[horizontalScrolling selectedCell] tag])
		{
		case 0:
			[object setHorizontalScrollButtonsEnabled: NO];
			[object setHorizontalScrollerEnabled: NO];
			[[verticalScrolling cellAt: 1 :0] setEnabled: YES];
			break;
		case 2:
			[object setHorizontalScrollButtonsEnabled: YES];
			[object setHorizontalScrollerEnabled: NO];
			[[verticalScrolling cellAt: 1 :0] setEnabled: YES];
			break;
		case 1:
		default:
			[object setHorizontalScrollerEnabled: YES];
			[object setHorizontalScrollButtonsEnabled: NO];
			[[verticalScrolling cellAt: 1 :0] setEnabled: NO];
			break;
		}
	
	return self;
}

		
- setVerticalScrolling
{
	switch ([[verticalScrolling selectedCell] tag])
		{
		case 1:
			[object useScrollButtons: YES];
			[object useScrollBars: NO];
			[[horizontalScrolling cellAt: 1 :0] setEnabled: NO];
			break;
		case 0:
		default:
			[object useScrollBars: YES];
			[object useScrollButtons: NO];
			[[horizontalScrolling cellAt: 1 :0] setEnabled: YES];
			break;
		}
	
	return self;
}

	
- setOptions
{
	[object setMultipleSelectionEnabled: ([[options cellAt: 0 : 0] state] ? YES : NO)];
	[object setEmptySelectionEnabled: ([[options cellAt: 1 : 0] state] ? YES : NO)];
	[object setBranchSelectionEnabled: ([[options cellAt: 2 : 0] state] ? YES : NO)];
	[object separateColumns: ([[options cellAt: 3 : 0] state] ? YES : NO)];

	if ([[options cellAt: 4 : 0] state])
		{
		[object setTitled: YES];
		[[options cellAt: 3 :0] setState: 1];
		[[options cellAt: 3 :0] setEnabled: NO];
		}
	else
		{
		[object setTitled: NO];
		[[options cellAt: 3 :0] setEnabled: YES];
		}
		
	return self;
}


- setCaseSensitiveSearch
{
	[object wantsCaseSensitiveSearch: [caseSensitiveSearch state]];
	return self;
}

- setInputTimeOut
{
	[object inputTimeOut: [inputTimeOut floatValue]];
	return self;
}


@end		